﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SinsenWeb.Helpers;
using SinsenWeb.Helpers.Filters.ControllerFilters;
using SinsenWeb.Models;

namespace SinsenWeb.Controllers
{
    [AdminAction]
    public class InstrumentController: BaseController
    {
        public ActionResult List()
        {
            var instruments = Instrument.FindAll();
            return View(instruments);
        }

        public ActionResult Index()
        {
            return RedirectToAction("List");
        }

        public ActionResult New()
        {
            var instrument = new Instrument();
            return View(instrument);
        }

        public ActionResult Edit(int id)
        {
            var instrument = Instrument.Find(id);
            return View(instrument);
        }

        public ActionResult Create( Instrument instrument)
        {
            if(ModelState.IsValid)
            {
                instrument.CreateAndFlush();
                MessageHelper.Message("Instrument lagt til");
            }
            return RedirectToAction("List");
        }
        
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Delete(int id)
        {
            var instrument = Instrument.Find(id);
            instrument.DeleteAndFlush();
            MessageHelper.Message(instrument.Name+ " slettet");
            return RedirectToAction("List");
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Update(Instrument instrument)
        {
            if(ModelState.IsValid)
            {
                instrument.SaveAndFlush();
                MessageHelper.Message("Instrument oppdatert");
            }
            return RedirectToAction<InstrumentController>(c=>c.List());
        }
    }
}
